/*
 * InvActivity displays a list of fire extinguishers within a specified building
 */
package com.example.wmuqrreader;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class InvActivity extends Activity {
	
	DatabaseHandler db;
	String building_id;
	
	ListView list;
	
	Button button_new;
	Button button_scan;	
	
	TextView building;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_inv_list);
		
		building_id = getIntent().getExtras().getString("BUILDING");
		
		
		Log.d("GOT: ", "BUILDING ID " + building_id);
		
		button_new 		= (Button) findViewById(R.id.btn_new);   
		button_scan 	= (Button) findViewById(R.id.btn_scan);
		
		building 		= (TextView) findViewById(R.id.textView_invTitle);
		
		db 				= new DatabaseHandler(this);
		list 			= (ListView) findViewById(R.id.listView1);
	
		//Read the name of the building for the banner
		building.setText( db.getBuilding(Integer.parseInt(building_id)).get_building() + " - Inventory List");
		
        List<Inventory> inventory = new ArrayList<Inventory>();
        inventory = db.getAllInventory(Integer.parseInt(building_id)); 

        CustomInventoryListViewAdapter adapter = new CustomInventoryListViewAdapter(this, R.layout.simple_list_inventory, inventory);        
        
        //Add a function for the EDIT row click
        list.setAdapter(adapter);       
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
            	Intent intent = new Intent();
            	
            	intent.putExtra("ID", (String) ((TextView) v.findViewById(R.id.textView_id)).getText());
            	intent.putExtra("BUILDING", building_id);
				
				intent.setClass(InvActivity.this, TallActivity.class);
		        startActivityForResult(intent, 0);
            }
        });
           
        //Add a function for the NEW button click          
        button_new.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				
				intent.putExtra("BUILDING", building_id);
				
				intent.setClass(InvActivity.this, TallActivity.class);
		        startActivityForResult(intent, 0);		        
			}
		});
        
        //Add a function for the SCAN button click
        button_scan.setOnClickListener(new View.OnClickListener() {
                        
                @Override
                public void onClick(View v) {
                	
                	//Start the editor activity
                	Intent intent = new Intent();    				
                	
    		        //Start the scanner activity
                    intent = new Intent("com.google.zxing.client.android.SCAN");
                    intent.setPackage("com.google.zxing.client.android");
                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                    startActivityForResult(intent, 1);                     
                }
         });
        
        db.close();
	}
	
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) 
        {
        	if (resultCode == RESULT_OK) 
            {
        		//Refresh the Inventory List activity to display the updated data     		
        		Intent refresh = new Intent();        		
        		refresh.setClass(this, InvActivity.class);
        		
        		refresh.putExtra("BUILDING", building_id);
        		
        		startActivity(refresh);
        		this.finish();
            }
        }
        
        if (requestCode == 1 ) 
        {
            if (resultCode == RESULT_OK) 
            {
            	Intent gotInventory = new Intent();   
                String contents = intent.getStringExtra("SCAN_RESULT");   
                
                Inventory inventory_scan = db.getInventory( Integer.parseInt(contents), "QR" ); 
                if(inventory_scan.get_inv() == -1)
                {
                	Toast.makeText(getApplicationContext(), "Creating New...", Toast.LENGTH_LONG).show();
                	gotInventory.putExtra("BUILDING", building_id);
                }
                else
                {
                	 Toast.makeText(getApplicationContext(), "Got: " + contents, Toast.LENGTH_LONG).show();
                	 int id = inventory_scan.get_id();
                	 gotInventory.putExtra("ID", Integer.toString(inventory_scan.get_id()));  
                	 gotInventory.putExtra("BUILDING", Integer.toString(inventory_scan.get_building()));  
                }
                
	           	gotInventory.setClass(InvActivity.this, TallActivity.class);
	            startActivity(gotInventory);
            } 
            else if (resultCode == RESULT_CANCELED) 
            {
                // Handle cancel
            }
        }
    }

}
